﻿--Aller chercher le NodeId en fonction de certains critères
SELECT *
  FROM [medianamik_Type]
  where Name = 'Comment'
  
-- Type a insérer
DECLARE @TypeId UNIQUEIDENTIFIER
SELECT @TypeId = '8A18ED50-3AD7-49C3-A13E-C187C756318A'

-- In sertion dans la table Type
SELECT 
'IF NOT EXISTS( SELECT NULL  FROM medianamik_Type where TypeId = ''' + CAST(TypeId as NVARCHAR(38)) + ''')
BEGIN
INSERT INTO [dbo].[medianamik_Type]
           ([TypeId]
           ,[ParentTypeId]
           ,[Name]
           ,[CanHaveChilds]
           ,[IsGenerated]
           ,[IsManyToMany]
           ,[Path]
           ,[Depth]
           ,[Roles]
           ,[Flags]
           ,[Description]
           ,[ExtendedProperties]
           ,[IsSystem]
           ,[IsAbstract])
     VALUES
           ('''+CAST(TypeId as nvarchar(38))+'''
           ,'+ISNULL(''''+CAST(ParentTypeId as NVARCHAR(38))+'''', 'NULL')+'
           ,'''+Name+'''
           ,'+STr(CanHaveChilds)+'
           ,'+STr(IsGenerated)+'
           ,'+STR(ISManyToMany)+'
           ,'''+Path+'''
           ,'+STR(Depth)+'
			,NULL
			,'+STR(Flags)+'
           ,'+ISNULL('''' + REPLACE(Description, '''', '''''' )+'''','NULL' )+'
			,'+ISNULL('''' + CAST(ExtendedProperties as NVARCHAR(MAX))+'''','NULL' )+'
          ,'+STR(IsSystem)+'
			,'+STR(IsAbstract)+'
           )
END'

 from medianamik_Type where typeId =@TypeID

--Insertion dans la table NodeInstance


--Insertion dans la table Property
union
SELECT 'IF NOT EXISTS( SELECT NULL FROM medianamik_Property where PropertyId = '''+cast(PropertyId as nvarchar(38))+''' )
BEGIN
INSERT INTO [dbo].[medianamik_Property]
           ([PropertyId]
           ,[TypeId]
           ,[Name]
           ,[HaveTooltip]
           ,[IsLanguageNeutral]
           ,[IsRequired]
           ,[IsUnique]
           ,[ValidationRegex]
           ,[GroupName]
           ,[Flags]
           ,[MedianamikControlId]
           ,[MedianamikControlWidth]
           ,[MedianamikControlHeight]
           ,[Position]
           ,[PrimaryPropertyId]
           ,[PrimaryTypeId]
           ,[PrimaryMedianamikControlId]
           ,[PrimaryMedianamikControlWidth]
           ,[PrimaryMedianamikControlHeight]
           ,[Roles]
           ,[MaxLength]
           ,[MinLength]
           ,[DataTypeId]
           ,[PrimaryDataTypeId]
           ,[Description]
           ,[TreeRootNodeId]
           , [PrimaryTreeRootNodeId])
     VALUES
           ('''+CAST(PropertyId as NVARCHAR(38))+'''
           ,'''+CAST(TypeId as NVARCHAR(38))+'''
			, '''+Name+'''
			,'+STR(HaveTooltip)+'
			,'+STR(IsLanguageNeutral)+'
			,'+STR(IsRequired)+'
			,'+STR(IsUnique)+'
			,'+ISNULL(''''+ValidationRegex+'''', 'NULL')+'
			,'+ISNULL(''''+GroupName+'''', 'NULL')+'
			,'+STR(Flags)+'
			,'+ISNULL(''''+CAST([MedianamikControlId] as NVARCHAR(38))+'''', 'NULL') + '
			,'+ISNULL(''''+[MedianamikControlWidth]+'''', 'null')+'
			,'+isnull(cast([MedianamikControlHeight] as varchar),'null')+'
			,'+STR(Position)+'
			,'+ISNULL(''''+CAST([PrimaryPropertyId] as NVARCHAR(38))+'''', 'NULL') +'
			,'+ISNULL(''''+CAST([PrimaryTypeId] as NVARCHAR(38))+'''', 'NULL') + '
			,'+ISNULL(''''+CAST([PrimaryMedianamikControlId] as NVARCHAR(38))+'''', 'NULL') + '
			,'+ISNULL(''''+[PrimaryMedianamikControlWidth]+'''', 'null')+'
			,'+isnull(cast([PrimaryMedianamikControlHeight] as varchar),'null')+'
			,'+ISNULL(''''+Roles+'''', 'null')+'
			,'+ISNULL(STR(MaxLength), 'NULL')+'
			,'+ISNULL(STR(MinLength), 'NULL')+'
			,'''+CAST(DataTypeId as NVARCHAR(38))+'''
			,'+ISNULL(''''+CAST([PrimaryDataTypeId] as NVARCHAR(38))+'''', 'NULL') + '
			,'+ISNULL(''''+REPLACE(Description, '''', '''''' )+'''', 'NULL')+'
			,'+ISNULL(''''+CAST([TreeRootNodeId] as NVARCHAR(38))+'''', 'NULL') + '
            ,'+ISNULL(''''+CAST([PrimaryTreeRootNodeId] as NVARCHAR(38))+'''', 'NULL') + '
           )
END'

from Medianamik_Property
where typeId=@TypeId

-- Extraction des règles hierarchiques

Select '
	if not exists(select typeId from [medianamik_HierarchicalRule] where TypeId = '''+cast(TypeId as varchar(38))+''' and ParentItemId = '''+cast(ParentItemId as varchar(38))+''')
	begin
		INSERT INTO [dbo].[medianamik_HierarchicalRule]
				   ([HierarchicalRuleId]
				   ,[TypeId]
				   ,[ParentItemId]
				   ,[ParentItemType])
			 VALUES
				   ('''+cast(HierarchicalRuleId as varchar(38))+'''
				   ,'''+cast(TypeId as varchar(38))+'''
				   ,'''+cast(ParentItemId as varchar(38))+'''
				   ,'+cast(ParentItemType as varchar(38))+')		
	end'
from [dbo].[medianamik_HierarchicalRule]
		where TypeId = @TypeId